/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reyes.mysite.dao;

import java.util.Date;
import java.util.List;

/**
 *
 * @author reyesyang
 */
public class GenericDAO<T> extends DAO {

    public void save(T t) {
        try {
            getPM().makePersistent(t);
        } catch (Exception e) {
            System.out.println("Can't save, for reason: " + e.getMessage());
        }
    }

    public T getById(Class C, Object id) {
        return (T) getPM().getObjectById(C, id);
    }

    public List<T> query(String queryString) {
        List<T> result = null;
        try {
            result = (List<T>) getPM().newQuery(queryString).execute();
        } catch (Exception e) {
            System.out.println("Can't excute query string for reason: " + e.getMessage());
        }
        return result;
    }

    public void delete(T t) {
        try {
            getPM().deletePersistent(t);
        } catch (Exception e) {
            System.out.println("Can't delete for reason: " + e.getMessage());
        }
    }

    public void delete(String queryString) {
        try {
            List<T> result = query(queryString);
            getPM().deletePersistentAll(result);
        } catch (Exception e) {
            System.out.println("Can't delete for reason: " + e.getMessage());
        }
    }

    public List<Date> queryDate(String queryString) {
        List<Date> result = null;
        try {
            result = (List<Date>) getPM().newQuery(queryString).execute();
        } catch (Exception e) {
            System.out.println("Can't delete for reason: " + e.getMessage());
        }
        return result;
    }
}
